﻿
namespace MyLibrary.API.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Reflection;

    public static class DataManager
    {
        private static Dictionary<string, IDatabase> DBS = new Dictionary<string, IDatabase>();

        public static IDatabase Get<DbType>(string name) where DbType : IDatabase
        {
            string connectionString = GetConnectionString(name);

            lock (DBS)
            {
                if (!DBS.ContainsKey(name))
                {
                    DbType database = (DbType) Activator.CreateInstance(typeof(DbType), new object[] { connectionString });                    
                    DBS.Add(name, database);
                }
            }

            return DBS[name];
        }

        private static string GetConnectionString(string name)
        {
            return ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }
    }
}
